import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { MsbmanageService } from './msbmanage.service';
import { CreateMsbmanageDto } from './dto/create-msbmanage.dto';
import { UpdateMsbmanageDto } from './dto/update-msbmanage.dto';

@Controller('msbmanage')
export class MsbmanageController {
  constructor(private readonly msbmanageService: MsbmanageService) {}

  @Post()
  create(@Body() createMsbmanageDto: CreateMsbmanageDto) {
    return this.msbmanageService.create(createMsbmanageDto);
  }

  @Get()
  findAll() {
    return this.msbmanageService.findAll();
  }

  @Get('search')
  searchGet(
    @Query('keyword') keyword: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    const pageNum = page ? Math.max(1, parseInt(page, 10) || 1) : 1;
    const pageSize = limit
      ? Math.max(1, Math.min(parseInt(limit, 10) || 50, 1000))
      : 50;

    return this.msbmanageService.searchByKeyword(
      keyword || '',
      pageNum,
      pageSize,
    );
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.msbmanageService.findOne(+id);
  }

  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateMsbmanageDto: UpdateMsbmanageDto,
  ) {
    return this.msbmanageService.update(+id, updateMsbmanageDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.msbmanageService.remove(+id);
  }

  @Post('search')
  search(
    @Body('keyword') keyword: string,
    @Body('page') page: number = 1,
    @Body('limit') limit: number = 50,
  ) {
    return this.msbmanageService.searchByKeyword(keyword, page, limit);
  }
}
